﻿namespace TestTool.GUI.Controls
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Drawing;
    using System.Windows.Forms;
    using TestTool.GUI.Controllers;
    using TestTool.GUI.Data;
    using TestTool.GUI.Enums;
    using TestTool.GUI.Views;
    using TestTool.Tests.Definitions.Interfaces;

    internal class ManagementPage : Page, IManagementView, IView
    {
        private ManagementController _controller;
        private Profile _currentProfile;
        private TestTool.GUI.Enums.ApplicationState _currentState;
        private Button btnApplyProfile;
        private Button btnLoad;
        private Button btnSaveAsXmlFile;
        private Button btnSaveCurrentOptions;
        private CheckBox chkOpenInNotepad;
        private IContainer components;
        private GroupBox gbProfiles;
        private GroupBox gbSaveSettings;
        private GroupBox gbXml;
        private Label lblProfileName;
        private Label lblProfilesList;
        private ListBox lbProfiles;
        private Panel panelLeft;
        private Panel panelSettings;
        private SettingsControl scSettings;
        private TextBox tbProfileName;
        private ToolTip toolTip;

        public ManagementPage()
        {
            this.InitializeComponent();
            this._controller = new ManagementController(this);
            this.scSettings.GetPTZNodes += new EventHandler(this.scSettings_GetPTZNodes);
            this.scSettings.GetEventTopics += new EventHandler(this.scSettings_GetEventTopics);
            this.scSettings.SetSecureMethods(this.Controller.SecureOperations());
            this.scSettings.GetVideoSources += new EventHandler(this.scSettings_GetVideoSources);
            this.scSettings.GetRecordings += new EventHandler(this.scSettings_GetRecordings);
        }

        private void ApplyProfile(Profile profile)
        {
            this._currentProfile = profile;
            this.scSettings.RebootTimeout = profile.Reboot;
            this.scSettings.MessageTimeout = profile.Message;
            this.scSettings.TimeBetweenTests = profile.InterTests;
            this.scSettings.OperationDelay = profile.OperationDelay;
            this.scSettings.RecoveryDelay = profile.RecoveryDelay;
            this.scSettings.DnsIpv4 = profile.DnsIpv4;
            this.scSettings.NtpIpv4 = profile.NtpIpv4;
            this.scSettings.GatewayIpv4 = profile.GatewayIpv4;
            this.scSettings.DnsIpv6 = profile.DnsIpv6;
            this.scSettings.NtpIpv6 = profile.NtpIpv6;
            this.scSettings.GatewayIpv6 = profile.GatewayIpv6;
            this.scSettings.UseEmbeddedPassword = profile.UseEmbeddedPassword;
            this.scSettings.Password1 = profile.Password1;
            this.scSettings.Password2 = profile.Password2;
            this.scSettings.SecureMethod = profile.SecureMethod;
            this.scSettings.PTZNodeToken = profile.PTZNodeToken;
            this.scSettings.VideoSourceToken = profile.VideoSourceToken;
            this.scSettings.EventTopic = profile.EventTopic;
            this.scSettings.TopicNamespaces = profile.TopicNamespaces;
            this.scSettings.SubscriptionTimeout = profile.SubscriptionTimeout;
            this.scSettings.RelayOutputDelayTimeMonostable = profile.RelayOutputDelayTime;
            this._controller.ApplyProfile(profile);
        }

        private void btnApplyProfile_Click(object sender, EventArgs e)
        {
            if (this.lbProfiles.SelectedItem != null)
            {
                try
                {
                    Profile selectedItem = (Profile) this.lbProfiles.SelectedItem;
                    this.ApplyProfile(selectedItem);
                }
                catch (Exception)
                {
                }
            }
        }

        private void btnLoad_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog {
                DefaultExt = ".xml",
                Filter = "XML file | *.xml | All Files | *.*"
            };
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                this._controller.LoadSettingsFromXML(dialog.FileName);
            }
        }

        private void btnSaveAsXml_Click(object sender, EventArgs e)
        {
            SaveFileDialog dialog = new SaveFileDialog {
                DefaultExt = ".xml",
                Filter = "XML file | *.xml | All Files | *.*",
                AddExtension = true
            };
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                this._controller.SaveAsXML(dialog.FileName);
            }
        }

        private void btnSaveCurrentOptions_Click(object sender, EventArgs e)
        {
            string text = this.tbProfileName.Text;
            if (string.IsNullOrEmpty(text))
            {
                MessageBox.Show("Profile name should not be empty!", "Profile name is incorrect");
            }
            else
            {
                Profile profile = this._controller.SaveCurrentProfile(text);
                this.lbProfiles.DataSource = null;
                this.lbProfiles.DisplayMember = "Name";
                this.lbProfiles.DataSource = this._controller.Profiles;
                this.lbProfiles.SelectedItem = profile;
            }
        }

        private void DisablePage()
        {
            base.DisableControls(new Control[] { this.lbProfiles, this.tbProfileName, this.btnApplyProfile, this.btnSaveCurrentOptions });
            this.scSettings.DisableControl();
        }

        private void DisplayCurrentProfile()
        {
            if (this.lbProfiles.SelectedItem != null)
            {
                Profile selectedItem = (Profile) this.lbProfiles.SelectedItem;
                this.DisplayProfile(selectedItem);
            }
        }

        private void DisplayProfile(Profile profile)
        {
            this.tbProfileName.Text = profile.Name;
        }

        public void DisplayProfiles(List<Profile> profiles)
        {
            this.InternalDisplayProfiles(profiles);
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void EnablePage()
        {
            base.EnableControls(new Control[] { this.lbProfiles, this.tbProfileName, this.btnSaveCurrentOptions });
            this.scSettings.EnableControl();
            this.scSettings.EnablePasswords(this._currentState);
            this.btnApplyProfile.Enabled = this.lbProfiles.SelectedItem != null;
        }

        public override IController GetController()
        {
            return this._controller;
        }

        private void InitializeComponent()
        {
            this.components = new Container();
            ComponentResourceManager manager = new ComponentResourceManager(typeof(ManagementPage));
            this.gbProfiles = new GroupBox();
            this.btnApplyProfile = new Button();
            this.lblProfilesList = new Label();
            this.lbProfiles = new ListBox();
            this.tbProfileName = new TextBox();
            this.lblProfileName = new Label();
            this.btnSaveCurrentOptions = new Button();
            this.toolTip = new ToolTip(this.components);
            this.gbSaveSettings = new GroupBox();
            this.panelSettings = new Panel();
            this.gbXml = new GroupBox();
            this.btnLoad = new Button();
            this.chkOpenInNotepad = new CheckBox();
            this.btnSaveAsXmlFile = new Button();
            this.panelLeft = new Panel();
            this.scSettings = new SettingsControl();
            this.gbProfiles.SuspendLayout();
            this.gbSaveSettings.SuspendLayout();
            this.panelSettings.SuspendLayout();
            this.gbXml.SuspendLayout();
            this.panelLeft.SuspendLayout();
            base.SuspendLayout();
            this.gbProfiles.Controls.Add(this.btnApplyProfile);
            this.gbProfiles.Controls.Add(this.lblProfilesList);
            this.gbProfiles.Controls.Add(this.lbProfiles);
            this.gbProfiles.Dock = DockStyle.Fill;
            this.gbProfiles.Location = new Point(0, 0x36);
            this.gbProfiles.Name = "gbProfiles";
            this.gbProfiles.Size = new Size(0x16a, 0x170);
            this.gbProfiles.TabIndex = 4;
            this.gbProfiles.TabStop = false;
            this.gbProfiles.Text = "Load Settings";
            this.btnApplyProfile.Anchor = AnchorStyles.Left | AnchorStyles.Bottom;
            this.btnApplyProfile.Enabled = false;
            this.btnApplyProfile.Location = new Point(9, 0x14c);
            this.btnApplyProfile.Name = "btnApplyProfile";
            this.btnApplyProfile.Size = new Size(0x81, 0x17);
            this.btnApplyProfile.TabIndex = 5;
            this.btnApplyProfile.Text = "Load Settings";
            this.toolTip.SetToolTip(this.btnApplyProfile, "Apply currently selected settings");
            this.btnApplyProfile.UseVisualStyleBackColor = true;
            this.btnApplyProfile.Click += new EventHandler(this.btnApplyProfile_Click);
            this.lblProfilesList.AutoSize = true;
            this.lblProfilesList.Location = new Point(9, 15);
            this.lblProfilesList.Name = "lblProfilesList";
            this.lblProfilesList.Size = new Size(0x43, 13);
            this.lblProfilesList.TabIndex = 3;
            this.lblProfilesList.Text = "Settings List:";
            this.lbProfiles.Anchor = AnchorStyles.Right | AnchorStyles.Left | AnchorStyles.Bottom | AnchorStyles.Top;
            this.lbProfiles.DisplayMember = "Name";
            this.lbProfiles.FormattingEnabled = true;
            this.lbProfiles.HorizontalScrollbar = true;
            this.lbProfiles.Location = new Point(9, 0x20);
            this.lbProfiles.Name = "lbProfiles";
            this.lbProfiles.Size = new Size(0x15b, 290);
            this.lbProfiles.TabIndex = 4;
            this.lbProfiles.SelectedIndexChanged += new EventHandler(this.lbProfiles_SelectedIndexChanged);
            this.tbProfileName.Anchor = AnchorStyles.Right | AnchorStyles.Left | AnchorStyles.Top;
            this.tbProfileName.Location = new Point(0x5b, 0x17);
            this.tbProfileName.Name = "tbProfileName";
            this.tbProfileName.Size = new Size(0xbc, 20);
            this.tbProfileName.TabIndex = 6;
            this.tbProfileName.TextChanged += new EventHandler(this.tbProfileName_TextChanged);
            this.lblProfileName.AutoSize = true;
            this.lblProfileName.Location = new Point(6, 0x1a);
            this.lblProfileName.Name = "lblProfileName";
            this.lblProfileName.Size = new Size(0x4f, 13);
            this.lblProfileName.TabIndex = 5;
            this.lblProfileName.Text = "Settings Name:";
            this.btnSaveCurrentOptions.Anchor = AnchorStyles.Right | AnchorStyles.Top;
            this.btnSaveCurrentOptions.Location = new Point(0x11d, 0x15);
            this.btnSaveCurrentOptions.Name = "btnSaveCurrentOptions";
            this.btnSaveCurrentOptions.Size = new Size(0x47, 0x17);
            this.btnSaveCurrentOptions.TabIndex = 7;
            this.btnSaveCurrentOptions.Text = "Save";
            this.toolTip.SetToolTip(this.btnSaveCurrentOptions, "Save current options");
            this.btnSaveCurrentOptions.UseVisualStyleBackColor = true;
            this.btnSaveCurrentOptions.Click += new EventHandler(this.btnSaveCurrentOptions_Click);
            this.gbSaveSettings.Controls.Add(this.lblProfileName);
            this.gbSaveSettings.Controls.Add(this.btnSaveCurrentOptions);
            this.gbSaveSettings.Controls.Add(this.tbProfileName);
            this.gbSaveSettings.Dock = DockStyle.Top;
            this.gbSaveSettings.Location = new Point(0, 0);
            this.gbSaveSettings.Name = "gbSaveSettings";
            this.gbSaveSettings.Size = new Size(0x16a, 0x36);
            this.gbSaveSettings.TabIndex = 3;
            this.gbSaveSettings.TabStop = false;
            this.gbSaveSettings.Text = "Save Current Settings";
            this.panelSettings.Controls.Add(this.gbProfiles);
            this.panelSettings.Controls.Add(this.gbSaveSettings);
            this.panelSettings.Controls.Add(this.gbXml);
            this.panelSettings.Dock = DockStyle.Fill;
            this.panelSettings.Location = new Point(0, 0);
            this.panelSettings.Name = "panelSettings";
            this.panelSettings.Size = new Size(0x16a, 500);
            this.panelSettings.TabIndex = 3;
            this.gbXml.Controls.Add(this.btnLoad);
            this.gbXml.Controls.Add(this.chkOpenInNotepad);
            this.gbXml.Controls.Add(this.btnSaveAsXmlFile);
            this.gbXml.Dock = DockStyle.Bottom;
            this.gbXml.Location = new Point(0, 0x1a6);
            this.gbXml.Name = "gbXml";
            this.gbXml.Size = new Size(0x16a, 0x4e);
            this.gbXml.TabIndex = 5;
            this.gbXml.TabStop = false;
            this.gbXml.Text = "XML Settings file";
            this.btnLoad.Location = new Point(9, 0x31);
            this.btnLoad.Name = "btnLoad";
            this.btnLoad.Size = new Size(0x81, 0x17);
            this.btnLoad.TabIndex = 2;
            this.btnLoad.Text = "Load...";
            this.btnLoad.UseVisualStyleBackColor = true;
            this.btnLoad.Click += new EventHandler(this.btnLoad_Click);
            this.chkOpenInNotepad.AutoSize = true;
            this.chkOpenInNotepad.Location = new Point(0x90, 0x18);
            this.chkOpenInNotepad.Name = "chkOpenInNotepad";
            this.chkOpenInNotepad.Size = new Size(0x6b, 0x11);
            this.chkOpenInNotepad.TabIndex = 1;
            this.chkOpenInNotepad.Text = "Open in Notepad";
            this.chkOpenInNotepad.UseVisualStyleBackColor = true;
            this.btnSaveAsXmlFile.Location = new Point(9, 20);
            this.btnSaveAsXmlFile.Name = "btnSaveAsXmlFile";
            this.btnSaveAsXmlFile.Size = new Size(0x81, 0x17);
            this.btnSaveAsXmlFile.TabIndex = 0;
            this.btnSaveAsXmlFile.Text = "Save current settings...";
            this.btnSaveAsXmlFile.UseVisualStyleBackColor = true;
            this.btnSaveAsXmlFile.Click += new EventHandler(this.btnSaveAsXml_Click);
            this.panelLeft.Controls.Add(this.panelSettings);
            this.panelLeft.Dock = DockStyle.Left;
            this.panelLeft.Location = new Point(0, 0);
            this.panelLeft.Name = "panelLeft";
            this.panelLeft.Padding = new Padding(0, 0, 4, 0);
            this.panelLeft.Size = new Size(0x16e, 500);
            this.panelLeft.TabIndex = 1;
            this.scSettings.AdvancedSettings = (List<object>) manager.GetObject("scSettings.AdvancedSettings");
            this.scSettings.DnsIpv4 = "";
            this.scSettings.DnsIpv6 = "";
            this.scSettings.Dock = DockStyle.Fill;
            this.scSettings.EventTopic = "";
            this.scSettings.GatewayIpv4 = "";
            this.scSettings.GatewayIpv6 = "";
            this.scSettings.Location = new Point(0x16e, 0);
            this.scSettings.MessageTimeout = 0;
            this.scSettings.MetadataFilter = "";
            this.scSettings.Name = "scSettings";
            this.scSettings.NtpIpv4 = "";
            this.scSettings.NtpIpv6 = "";
            this.scSettings.OperationDelay = 0;
            this.scSettings.Password1 = "OnvifTest123";
            this.scSettings.Password2 = "OnvifTest321";
            this.scSettings.PTZNodeToken = "";
            this.scSettings.RebootTimeout = 0;
            this.scSettings.RecordingToken = "";
            this.scSettings.RecoveryDelay = 0;
            this.scSettings.RelayOutputDelayTimeMonostable = 0;
            this.scSettings.SearchTimeout = 0;
            this.scSettings.SecureMethod = "";
            this.scSettings.Size = new Size(0x1af, 500);
            this.scSettings.SubscriptionTimeout = 0;
            this.scSettings.TabIndex = 4;
            this.scSettings.TimeBetweenTests = 0;
            this.scSettings.TopicNamespaces = "";
            this.scSettings.UseEmbeddedPassword = true;
            this.scSettings.VideoSourceToken = "";
            base.AutoScaleDimensions = new SizeF(6f, 13f);
            base.AutoScaleMode = AutoScaleMode.Font;
            base.Controls.Add(this.scSettings);
            base.Controls.Add(this.panelLeft);
            base.Name = "ManagementPage";
            base.Size = new Size(0x31d, 500);
            this.gbProfiles.ResumeLayout(false);
            this.gbProfiles.PerformLayout();
            this.gbSaveSettings.ResumeLayout(false);
            this.gbSaveSettings.PerformLayout();
            this.panelSettings.ResumeLayout(false);
            this.gbXml.ResumeLayout(false);
            this.gbXml.PerformLayout();
            this.panelLeft.ResumeLayout(false);
            base.ResumeLayout(false);
        }

        public void InternalDisplayProfiles(List<Profile> profiles)
        {
            this.lbProfiles.DisplayMember = "Name";
            this.lbProfiles.DataSource = profiles;
            this.lbProfiles.SelectedItem = null;
            this.tbProfileName.Text = string.Empty;
        }

        private void lbProfiles_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.DisplayCurrentProfile();
            this.btnApplyProfile.Enabled = this.lbProfiles.SelectedItem != null;
        }

        private void scSettings_GetEventTopics(object sender, EventArgs e)
        {
            try
            {
                this._controller.QueryEventTopics();
            }
            catch (Exception exception)
            {
                this.ShowError(exception.Message);
            }
        }

        private void scSettings_GetPTZNodes(object sender, EventArgs e)
        {
            try
            {
                this._controller.GetPTZNodes();
            }
            catch (Exception exception)
            {
                this.ShowError(exception.Message);
            }
        }

        private void scSettings_GetRecordings(object sender, EventArgs e)
        {
            try
            {
                this._controller.QueryRecordings();
            }
            catch (Exception exception)
            {
                this.ShowError(exception.Message);
            }
        }

        private void scSettings_GetVideoSources(object sender, EventArgs e)
        {
            try
            {
                this._controller.QueryVideoSources();
            }
            catch (Exception exception)
            {
                this.ShowError(exception.Message);
            }
        }

        public void SetEventsTopic(List<EventsTopicInfo> topics)
        {
            this.scSettings.SetEventsTopic(topics);
        }

        public override void SwitchToState(TestTool.GUI.Enums.ApplicationState state)
        {
            Action method = null;
            Action action2 = null;
            this._currentState = state;
            this.scSettings.SwitchToState(state);
            switch (state)
            {
                case TestTool.GUI.Enums.ApplicationState.Idle:
                    if (action2 == null)
                    {
                        action2 = () => this.EnablePage();
                    }
                    base.BeginInvoke(action2);
                    return;

                case TestTool.GUI.Enums.ApplicationState.TestRunning:
                case TestTool.GUI.Enums.ApplicationState.TestPaused:
                case TestTool.GUI.Enums.ApplicationState.DiscoveryRunning:
                case TestTool.GUI.Enums.ApplicationState.CommandRunning:
                case TestTool.GUI.Enums.ApplicationState.ConformanceTestRunning:
                    if (method == null)
                    {
                        method = () => this.DisablePage();
                    }
                    base.BeginInvoke(method);
                    return;
            }
        }

        private void tbProfileName_TextChanged(object sender, EventArgs e)
        {
            this.toolTip.SetToolTip(this.tbProfileName, this.tbProfileName.Text);
        }

        internal ManagementController Controller
        {
            get
            {
                return this._controller;
            }
        }

        public Profile CurrentProfile
        {
            get
            {
                return this._currentProfile;
            }
        }

        public bool OpenFileForEditing
        {
            get
            {
                return this.chkOpenInNotepad.Checked;
            }
            set
            {
                this.chkOpenInNotepad.Checked = value;
            }
        }

        public ITestSettingsView SettingsView
        {
            get
            {
                return this.scSettings;
            }
        }
    }
}

